package csci4448.android.workouttracker;

import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class PersonalTabActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_personal_tab);
        populateFormFields();
    }

    public void onResume() {
        super.onResume();
        populateFormFields();
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_personal_tab, menu);
        return true;
    }
    
    public void populateFormFields()
    {
        SessionModel Model = new SessionModel(this);
        
		final TextView CumulativeTotalTimeValue = (TextView)findViewById(R.id.cumulativeTotalTimeValue);
		final TextView CumulativeTotalDistanceValue = (TextView)findViewById(R.id.cumulativeTotalDistanceValue);
		final TextView CumulativeAverageSpeedValue = (TextView)findViewById(R.id.cumulativeAvgSpeedValue);
		
		final TextView LongestSessionValue = (TextView)findViewById(R.id.longestSessionValue);
		final TextView FarthestSessionValue = (TextView)findViewById(R.id.farthestSessionValue);
		final TextView MaxSpeedValue = (TextView)findViewById(R.id.maxSpeedValue);
		
        try {
        	double TotalDistance = 0.0d;
        	long TotalDuration = 0;
        	
        	double MaxSpeed = 0.0d;
        	double MaxDistance = 0.0d;
        	long LongestSession = 0;
        	
			List<Session> Sessions = Model.getSessions();
			for(Session s: Sessions)
			{
				double SessionSpeed = s.getAverageSpeed();
				double SessionDistance = s.getStorageDistance();
				long SessionDuration = s.getDurationSeconds();
				TotalDistance += SessionDistance;
				TotalDuration += SessionDuration;
				if(SessionDuration > LongestSession)
				{
					LongestSession = SessionDuration;
				}
				if(SessionDistance > MaxDistance)
				{
					MaxDistance = SessionDistance;
				}
				if(SessionSpeed > MaxSpeed)
				{
					MaxSpeed = SessionSpeed;
				}
			}
			
			ImperialSession TotalSession = new ImperialSession(-1, TotalDuration, TotalDistance, null, null, null);
			ImperialSession MaxSession = new ImperialSession(-1, LongestSession, MaxDistance, null, null, null);
			
			CumulativeTotalTimeValue.setText(TotalSession.getDurationString());
			CumulativeTotalDistanceValue.setText(TotalSession.getDistanceString());
			CumulativeAverageSpeedValue.setText(TotalSession.getSpeedString());
			
			LongestSessionValue.setText(MaxSession.getDurationString());
			FarthestSessionValue.setText(MaxSession.getDistanceString());
			MaxSpeedValue.setText(MaxSession.getSpeedString(MaxSpeed));
		} catch (Exception e) {}
    }
}
